package com.mall.b2c.controller;

import com.mall.b2c.domain.card.CardDomain;
import com.mall.b2c.domain.intergral.ShopClassDomain;
import com.mall.b2c.service.CardService;
import com.mall.b2c.util.JsonDataGridHelper;
import com.mall.common.bean.Pager;
import com.mall.common.bean.SearchFilter;
import com.mall.common.controller.BaseController;
import com.mall.common.enums.OperateType;
import com.mall.common.exception.NoticeException;
import com.mall.common.log.Log;
import com.mall.common.util.ResponseUtils;
import com.mall.common.web.Servlets;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * Created by chen on 17/1/19.
 */
@Controller
@RequestMapping("/admin/b2c/b2c_card_list")
public class CardController extends BaseController {
    @Resource
    CardService cardService;

    /**
     * 跳转到列表页面
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Model model) {

        return "/admin/b2c/card/list";
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(Model model) {

        return "/admin/b2c/card/input";
    }

    @RequestMapping(value = "/jh", method = RequestMethod.POST)
    @ResponseBody
    public String downMarket(Long[] ids, HttpServletResponse response) {
        cardService.jh(ids);

        return ResponseUtils.renderJSON(successMessage, response);
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public String save(Long number, CardDomain cardDomain, HttpServletResponse response) {
        cardService.produceCard(number, cardDomain);
        return ResponseUtils.renderJSON(successMessage, response);
    }

    /**
     * 获取列表信息
     */
    @RequestMapping(value = "/queryData", method = RequestMethod.POST)
    @ResponseBody
    public Pager queryData(Pager pager, HttpServletRequest request) {
        Map<String, Object> filterParams = Servlets.getParametersStartingWith(request);
        List<SearchFilter> filters = SearchFilter.parse(filterParams);
        pager = cardService.findPage(pager, filters);
        return JsonDataGridHelper.createJSONData(pager);
    }

}
